﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace YUIAspNet.HtmlHelpers
{
    public static class YUIHelper
    {
        public static string Menu(object[] items)
        {
            StringBuilder menu = new StringBuilder();

            AddItemsRecursive(items, menu);

            

            //menu.AppendFormat("</ul");

            return menu.ToString();
        }

        private static void AddItemsRecursive(object[] items, StringBuilder menu)
        {
            menu.Append("<ul>");
            foreach (object item in items)
            {
                var props = ReflectionHelper.GetProperties(item);

                if (props.ContainsKey("text"))
                    AddMenuItem(props, menu);
            }
            menu.Append("</ul>");
        }

        private static void AddMenuItem(Dictionary<string, object> props, StringBuilder menu)
        {
            AddListItem((string)props["text"], menu);

            if (props.ContainsKey("items"))
                AddItemsRecursive((object[])props["items"], menu);
        }

        private static void AddListItem(string text, StringBuilder menu)
        {
            menu.AppendFormat("<li>{0}</li>", text);
        }

        private static void AddListItem()
        {
            throw new NotImplementedException();
        }
    }
}
